package com.flowpp.workflow.controller;

import com.flowpp.workflow.dto.StartBody;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author DON
 * @version 1.0
 * @date 2023/7/20
 */
@Slf4j
@RestController
@RequestMapping("flow")
public class WorkFlowController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @GetMapping("/deploy")
    public String deploy() {
//        Deployment sayHello = repositoryService.createDeployment()
//                .name("say hello") //定义部署文件的名称
//                .addClasspathResource("process.bpmn") //绑定需要部署的流程文件
//                .deploy();// 部署流程
//        return sayHello.getId() + " : " + sayHello.getName();
        Deployment deploy = repositoryService.createDeployment()
                .name("请假流程") //定义部署文件的名称
                .addClasspathResource("LeaveWorkflow.bpmn") //绑定需要部署的流程文件
                .deploy();// 部署流程
        return deploy.getId() + " : " + deploy.getName();

    }

    @PostMapping("/startFlow")
    public void startFlow(@RequestBody StartBody startBody) {
//        String processDefinitionKey = "camunda-process";
        String processDefinitionKey = "LeaveWorkflow";
        HashMap<String, Object> map = new HashMap<>();
        map.put("days", startBody.getDays() == null ? 1 : startBody.getDays());
        map.put("reason", startBody.getDays() == null ? "日常请假" : startBody.getReason());
        map.put("leader", startBody.getDays() == null ? "demo" : startBody.getLeader());
        map.put("initiator", startBody.getDays() == null ? "demo" : startBody.getInitiator());
        //部署流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);
        // 流程实例的相关信息
        System.out.println("processInstance.getId() : " + processInstance.getId());
        System.out.println("processInstance.getProcessDefinitionId() : " + processInstance.getProcessDefinitionId());
    }



}
